package com.ta.stage.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ta.stage.dao.UserDao;
import com.ta.stage.entity.Storage;
import com.ta.stage.entity.User;
import com.ta.stage.entity.vo.UserVo;
import com.ta.stage.result.Result;
import com.ta.stage.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/")
@Api(tags={"用户 UserController"})
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "查询用户")
    @RequestMapping(value = "findUser",method = RequestMethod.GET)
    @ResponseBody
    public Result findUser(){
        List<User> userList = userService.list(null);
        return Result.ok(userList);
    }

    @ApiOperation(value = "根据id查询用户")
    @GetMapping("getUser/{id}")
    public Result getUser(@PathVariable Long id){
        int num = 0;
        User user = userService.getById(id);
        return Result.ok(user);
    }

    @ApiOperation(value = "新增用户")
    @RequestMapping(value = "saveUser",method = RequestMethod.POST)
    @ResponseBody
    public Result saveUser(@RequestBody User user){
        boolean save = userService.save(user);
        return save?Result.ok():Result.fail();
    }

    @ApiOperation(value = "逻辑删除用户")
    @DeleteMapping("{id}")
    public Result removeUser(@PathVariable Long id){
        boolean flag = userService.removeById(id);
        return flag?Result.ok():Result.fail();
    }

    @ApiOperation(value = "修改用户")
    @PostMapping("updateUser")
    public Result updateUser(@RequestBody User user){
        boolean flag = userService.updateById(user);
        return flag?Result.ok():Result.fail();
    }

    @ApiOperation(value = "批量删除用户")
    @DeleteMapping("batchRemoveUser")
    public Result batchRemoveUser(@RequestBody List<Long> idList){
        userService.removeByIds(idList);
        return Result.ok();
    }

    @ApiOperation(value = "用户条件查询带分页")
    @PostMapping("findPageUser/{current}/{limit}")
    public Result findPageUser(@PathVariable long current,
                               @PathVariable long limit,
                               @RequestBody(required = false) UserVo userVo){

        //创建page对象，传递当前页，每页记录数
        Page<User> page = new Page<>(current,limit);

        //构建条件
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        Integer id = userVo.getId();
        String realname = userVo.getRealname();

        //条件可能为空
        if (!StringUtils.isEmpty(id)){
            wrapper.eq("id",userVo.getId());
        }
        if (!StringUtils.isEmpty(realname)){
            wrapper.like("name",userVo.getRealname());
        }
        //调用方法实现分页查询
        Page<User> pageDept = userService.page(page, wrapper);
        return Result.ok(pageDept);
    }
}
